package cn.yyx.orderservice.controller;


import cn.yyx.common.R;
import cn.yyx.customerservice.entity.Customer;
import cn.yyx.customerservice.service.impl.CustomerServiceImpl;
import cn.yyx.empservice.service.EmployeeService;
import cn.yyx.orderservice.entity.Order;
import cn.yyx.orderservice.entity.OrderQueryPageParam;
import cn.yyx.orderservice.service.OrderService;
import cn.yyx.staservice.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-10-25
 */
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private CustomerServiceImpl service;

//    @ApiOperation("分页列表")
//    @PostMapping("/list/{page}/{limit}")
//    public R list(@RequestBody OrderQueryPageParam param) {
//        return R.ok().data(orderService.list(param));
//    }


    @PostMapping("list/{page}/{limit}")
    public R orderList(@ApiParam(name = "page",value = "当前页码",required = true)
                       @PathVariable Long page,
                       @ApiParam(name = "limit",value ="每页记录数",required = true)
                       @PathVariable Long limit,
                       @RequestBody OrderQueryPageParam orderPageVo){
        Page<Order> pageParam = new Page<>(page,limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(orderPageVo.getCname())){
            wrapper.like("cname",orderPageVo.getCname());
        }
        if (!ObjectUtils.isEmpty(orderPageVo.getSaleId())){
            wrapper.like("sale_id",orderPageVo.getSaleId());
        }
        if (!ObjectUtils.isEmpty(orderPageVo.getOperatorId())){
            wrapper.like("operator_id",orderPageVo.getOperatorId());
        }
        if (!ObjectUtils.isEmpty(orderPageVo.getOrderStatus())){
            wrapper.eq("order_status",orderPageVo.getOrderStatus());
        }
        if(!ObjectUtils.isEmpty(orderPageVo.getBegin()) && !ObjectUtils.isEmpty(orderPageVo.getEnd()))
            wrapper.between("order_date",orderPageVo.getBegin(),orderPageVo.getEnd());
//        wrapper.select("date_format(order_date,'%Y-%m') as month,count(*) as count").between("date_format(order_date,'%Y-%m-%d')",orderPageVo.getBegin(),orderPageVo.getEnd())
//                .groupBy("date_format(order_date,'%Y-%m')").orderByAsc("month");
        wrapper.orderByDesc("order_status");
        orderService.page(pageParam,wrapper);
        List<Order> orders= pageParam.getRecords();
        for (Order order:orders){
            order.setSname(employeeService.getById(order.getSaleId()).getName());
            if (employeeService.getById(order.getOperatorId())!=null){
                order.setOperatorName(employeeService.getById(order.getOperatorId()).getName());
            }
            order.setCid(getCid(order.getCname()));
        }
        return R.ok().data("items",orders).data("total",pageParam.getTotal());
    }

    @GetMapping("/{id}")
    public R getOrderById(@PathVariable String id){
        return R.ok().data("item",orderService.getById(id));
    }

    @ApiOperation("添加列表")
    @PostMapping("/add")
    public R addOrder(@RequestBody Order order){
        order.setOrderDate(new Date());
        return  orderService.save(order)?R.ok():R.error();
    }

    @ApiOperation("修改客户")
    @PostMapping("/update")
    public R update(@RequestBody Order order){
        return orderService.updateById(order)?R.ok():R.error();
    }

    public String getCid(String name){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cname",name);
        Customer customer = service.getOne(queryWrapper);
        if (!ObjectUtils.isEmpty(customer)) {
            return customer.getCid();
        }else {
            return null;
        }
    }

}

